# encoding: utf-8
# from __future__ import division, print_function, unicode_literals
import objc
from GlyphsApp import *
from GlyphsApp.plugins import *
from vanilla import *
# from math import sin, cos, pi, radians
import math
#import numpy as np
#class Fontbold(FilterWithDialog):
class Huntingbody(FilterWithDialog):

    @objc.python_method
    def settings(self):
        self.menuName = Glyphs.localize({'zh': '猎捕体一键添加尖角'})
        #self.menuName = Glyphs.localize({'en': 'Add part'})

        # Create Vanilla window and group with controls
        width = 250
        height = 180
        self.paletteView = Window((width, height),closable=False)
        self.paletteView.group = Group((0, 0, width, height))
        self.paletteView.group.ClikUpCheckBox = CheckBox((120, 20, -20, 20), "上",
                           callback=self.clikUp_, value=False)
        self.paletteView.group.ClikDownCheckBox = CheckBox((120, -40, -20, 20), "下",
                           callback=self.clikDown_, value=False)
        self.paletteView.group.ClikLeftCheckBox = CheckBox((30, -100, -20, 20), "左",
                           callback=self.clikLeft_, value=False)
        self.paletteView.group.ClikRightCheckBox = CheckBox((200, -100, -20, 20), "右",
                           callback=self.clikRight_, value=False)
        #self.paletteView.group.textBox = TextBox((110, -130, -10, 20), '部件名')
        #self.paletteView.group.editText = EditText((100, -100, 60, 25), callback=self.editTextCallback, continuous=True)
        # Set dialog to NSView
        self.dialog = self.paletteView.group.getNSView()
    # Action triggered by UI
    # @objc.python_method
    # def editTextCallback(self, sender):
    #
    #     # Store value coming in from dialog
    #     Glyphs.defaults['com.jingmingtao.Addpart.part'] = sender.get()
    #
    #     # Trigger redraw
    #     #self.update()


    # ---------
    @objc.python_method
    def calculate_angle_with_horizontal(self, x1, y1, x2, y2):
        """
        计算线段与水平线的夹角（0-90度）
        :param x1, y1: 线段起点坐标
        :param x2, y2: 线段终点坐标
        :return: 线段与水平线的夹角（度数，0-90）
        """
        dx = x2 - x1
        dy = y2 - y1

        # 处理垂直线段（dx=0）的情况
        if dx == 0:
            return 90.0

        # 计算斜率角度（-90到90度）
        angle_rad = math.atan2(dy, dx)
        angle_deg = math.degrees(angle_rad)

        # 取绝对值得到0-90度的角度
        return abs(angle_deg)

    @objc.python_method
    def compare_segments_horizontal_closeness(self, p1, p2, p3):
        """
        比较两条线段哪个更接近水平
        :param p1, p2, p3: 三个连续点坐标 [(x1,y1), (x2,y2), (x3,y3)]
        :return: 返回更接近水平的线段索引（1或2），角度差
        """
        # 计算第一条线段（p1-p2）的角度
        angle1 = self.calculate_angle_with_horizontal(p1[0], p1[1], p2[0], p2[1])

        # 计算第二条线段（p2-p3）的角度
        angle2 = self.calculate_angle_with_horizontal(p2[0], p2[1], p3[0], p3[1])
        diff1 = min(angle1, 180 - angle1)  # 考虑0-180度对称性
        diff2 = min(angle2, 180 - angle2)
        return diff1, diff2

    @objc.python_method
    def calculate_new_point1(self, p1, p2, distance):
        dx = p2[0] - p1[0]
        dy = p2[1] - p1[1]

        length = math.sqrt(dx ** 2 + dy ** 2)

        if length == 0:
            return p1  # 两点重合

        unit_dx = dx / length
        unit_dy = dy / length

        new_x = p1[0] + unit_dx * distance
        new_y = p1[1] + unit_dy * distance

        return (new_x, new_y)

    @objc.python_method
    def calculate_new_point2(self, p1, p2, distance):
        dx = p2[0] - p1[0]
        dy = p2[1] - p1[1]

        length = math.sqrt(dx ** 2 + dy ** 2)

        if length == 0:
            return p1  # 两点重合

        unit_dx = dx / length
        unit_dy = dy / length

        new_x = p2[0] + unit_dx * distance
        new_y = p2[1] + unit_dy * distance

        return (new_x, new_y)

    @objc.python_method
    def calculate_points_xiangzuo(self, A, B):
        """
        计算点C和点D的坐标（修正版）
        :param A: 点A坐标 (x, y)
        :param B: 点B坐标 (x, y)
        :return: 点C和点D的坐标
        """
        # 计算AB的长度
        AB_length = math.sqrt((B[0] - A[0]) ** 2 + (B[1] - A[1]) ** 2)

        # 计算AC和BD的长度（AB的1/3）
        part_length = AB_length / 3

        # 对于垂直线段AB(936,845)->(936,718)
        # AB方向向量：(0, -127)

        # 左方向法向量应为(-1, 0)
        left_normal = (-1, 0)

        # 计算点C的坐标（A点左下方）
        # 从A点向下移动part_length，再向左移动22
        C_x = A[0] + left_normal[0] * 22
        C_y = A[1] - part_length  # 向下移动

        # 计算点D的坐标（B点左上方）
        # 从B点向上移动part_length，再向左移动22
        D_x = B[0] + left_normal[0] * 22
        D_y = B[1] + part_length  # 向上移动

        return (round(C_x, 2), round(C_y, 2)), (round(D_x, 2), round(D_y, 2))

    @objc.python_method
    def calculate_points_xiangyou(self, A, B):
        """
        计算点C和点D的坐标（完全满足要求版）
        :param A: 点A坐标 (x, y)
        :param B: 点B坐标 (x, y)
        :return: 点C和点D的坐标
        """
        # 计算AB向量
        dx = B[0] - A[0]
        dy = B[1] - A[1]
        AB_length = math.sqrt(dx ** 2 + dy ** 2)

        # 计算单位向量
        AB_unit = (dx / AB_length, dy / AB_length)

        # 计算右方向法向量（AB顺时针旋转90度）
        normal_right = (AB_unit[1], -AB_unit[0])

        # 计算点C（A点右下方）
        # 1. 沿AB方向移动AB/3
        # 2. 向右移动22
        C_x = A[0] + (AB_length / 3) * AB_unit[0] + 22 * normal_right[0]
        C_y = A[1] + (AB_length / 3) * AB_unit[1] + 22 * normal_right[1]

        # 确保C在A的右下方（x增大，y减小）
        if not (C_x > A[0] and C_y < A[1]):
            # 如果不符合，则取反法向量
            C_x = A[0] + (AB_length / 3) * AB_unit[0] - 22 * normal_right[0]
            C_y = A[1] + (AB_length / 3) * AB_unit[1] - 22 * normal_right[1]

        # 计算点D（B点右上方）
        # 1. 沿BA方向移动AB/3
        # 2. 向右移动22
        D_x = B[0] - (AB_length / 3) * AB_unit[0] + 22 * normal_right[0]
        D_y = B[1] - (AB_length / 3) * AB_unit[1] + 22 * normal_right[1]

        # 确保D在B的右上方（x增大，y增大）
        if not (D_x > B[0] and D_y > B[1]):
            # 如果不符合，则取反法向量
            D_x = B[0] - (AB_length / 3) * AB_unit[0] - 22 * normal_right[0]
            D_y = B[1] - (AB_length / 3) * AB_unit[1] - 22 * normal_right[1]

        return (round(C_x, 2), round(C_y, 2)), (round(D_x, 2), round(D_y, 2))

    @objc.python_method
    def distance_crosstwopoins(self, p1, p2):
        """计算两点之间的欧几里得距离"""
        return math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)

    @objc.python_method
    def sqrt_diff_safe(self, a, b):
        """安全的√(a² - b²)计算，处理负数情况"""
        diff = a ** 2 - b ** 2
        if diff < 0:
            raise ValueError("a²必须大于或等于b²，否则结果为虚数")
        return math.sqrt(diff)

    @objc.python_method
    def clikUp_(self, sender1):
        Glyphs.defaults['com.jingmingtao.Addpart.up']= bool(sender1.get())
        if Glyphs.defaults['com.jingmingtao.Addpart.up']==True:
            self.paletteView.group.ClikDownCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.down'] = False
            self.paletteView.group.ClikLeftCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.left'] = False
            self.paletteView.group.ClikRightCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.right'] = False
        #self.update()
    @objc.python_method
    def clikDown_(self, sender2):
        Glyphs.defaults['com.jingmingtao.Addpart.down'] = bool(sender2.get())
        if Glyphs.defaults['com.jingmingtao.Addpart.down']==True:
            self.paletteView.group.ClikUpCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.up'] = False
            self.paletteView.group.ClikLeftCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.left'] = False
            self.paletteView.group.ClikRightCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.right'] = False
        self.update()
    @objc.python_method
    def clikLeft_(self, sender3):
        Glyphs.defaults['com.jingmingtao.Addpart.left'] = bool(sender3.get())
        if Glyphs.defaults['com.jingmingtao.Addpart.left'] == True:
            self.paletteView.group.ClikUpCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.up'] = False
            self.paletteView.group.ClikDownCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.down'] = False
            self.paletteView.group.ClikRightCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.right'] = False
        self.update()
    @objc.python_method
    def clikRight_(self, sender4):
        Glyphs.defaults['com.jingmingtao.Addpart.right'] = bool(sender4.get())
        if Glyphs.defaults['com.jingmingtao.Addpart.right'] == True:
            self.paletteView.group.ClikUpCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.up'] = False
            self.paletteView.group.ClikDownCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.down'] = False
            self.paletteView.group.ClikLeftCheckBox.set(False)
            Glyphs.defaults['com.jingmingtao.Addpart.left'] = False
        self.update()
    # Actual filter
    @objc.python_method
    def filter(self, Layer, inEditView, customParameters):
        # for layer in Glyphs.font.selectedLayers:
        #     for path in layer.paths:
        # 找到选中的边和所在路径
        selected_nodes = []
        selected_path = None
        for path in Layer.paths:
            print("path:", path)
            for i, node in enumerate(path.nodes):
                if node.selected:
                    selected_nodes.append(node)
                    selected_path = path  # 保存路径引用
            # 定义多边形顶点（逆时针顺序）
            print("selected_path.nodes:", selected_path)

        if len(selected_nodes) == 2:

            zhongdian_quyu_x = selected_path.bounds.origin.x + selected_path.bounds.size.width / 2
            zhongdian_xianduan_x = (selected_nodes[0].x + selected_nodes[1].x) / 2
            print("zhongdian_quyu_x:", zhongdian_quyu_x)
            print("zhongdian_xianduan_x:", zhongdian_xianduan_x)
            if zhongdian_xianduan_x > zhongdian_quyu_x:
                if selected_nodes[0].y > selected_nodes[1].y:
                    pointA = (selected_nodes[0].x, selected_nodes[0].y)
                    pointB = (selected_nodes[1].x, selected_nodes[1].y)
                if selected_nodes[0].y < selected_nodes[1].y:
                    pointA = (selected_nodes[1].x, selected_nodes[1].y)
                    pointB = (selected_nodes[0].x, selected_nodes[0].y)
                point_C, point_D = self.calculate_points_xiangzuo(pointA, pointB)
                print("point_C,point_D:", point_C, point_D)
                if selected_nodes[0].index == 0 and selected_nodes[1].index == len(selected_path.nodes) - 1:
                    print("4444444:")
                    if selected_nodes[0].y < selected_nodes[1].y:
                        print("777777777777:")
                        newNode1 = GSNode(point_C)
                        newNode2 = GSNode(point_D)
                        newNode1.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[1].index + 1, newNode1)
                        newNode2.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[1].index + 2, newNode2)
                        selected_nodes[0].type = CURVE

                    if selected_nodes[0].y > selected_nodes[1].y:
                        newNode1 = GSNode(point_D)
                        newNode2 = GSNode(point_C)
                        newNode1.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[1].index + 1, newNode1)
                        newNode2.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[1].index + 2, newNode2)
                        selected_nodes[0].type = CURVE
                else:
                    if selected_nodes[0].y < selected_nodes[1].y:
                        newNode1 = GSNode(point_D)
                        newNode2 = GSNode(point_C)
                        newNode1.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[0].index + 1, newNode1)
                        newNode2.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[0].index + 2, newNode2)
                        selected_nodes[1].type = CURVE
                    if selected_nodes[0].y > selected_nodes[1].y:
                        newNode1 = GSNode(point_C)
                        newNode2 = GSNode(point_D)
                        newNode1.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[0].index + 1, newNode1)
                        newNode2.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[0].index + 2, newNode2)
                        selected_nodes[1].type = CURVE

            if zhongdian_xianduan_x < zhongdian_quyu_x:
                if selected_nodes[0].y > selected_nodes[1].y:
                    pointA = (selected_nodes[0].x, selected_nodes[0].y)
                    pointB = (selected_nodes[1].x, selected_nodes[1].y)
                if selected_nodes[0].y < selected_nodes[1].y:
                    pointA = (selected_nodes[1].x, selected_nodes[1].y)
                    pointB = (selected_nodes[0].x, selected_nodes[0].y)
                point_C, point_D = self.calculate_points_xiangyou(pointA, pointB)
                print("point_C,point_D:", point_C, point_D)
                if selected_nodes[0].index == 0 and selected_nodes[1].index == len(selected_path.nodes) - 1:
                    print("4444444:")
                    if selected_nodes[0].y < selected_nodes[1].y:
                        print("777777777777:")
                        newNode1 = GSNode(point_C)
                        newNode2 = GSNode(point_D)
                        newNode1.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[1].index + 1, newNode1)
                        newNode2.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[1].index + 2, newNode2)
                        selected_nodes[0].type = CURVE

                    if selected_nodes[0].y > selected_nodes[1].y:
                        newNode1 = GSNode(point_D)
                        newNode2 = GSNode(point_C)
                        newNode1.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[1].index + 1, newNode1)
                        newNode2.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[1].index + 2, newNode2)
                        selected_nodes[0].type = CURVE
                else:
                    if selected_nodes[0].y < selected_nodes[1].y:
                        newNode1 = GSNode(point_D)
                        newNode2 = GSNode(point_C)
                        newNode1.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[0].index + 1, newNode1)
                        newNode2.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[0].index + 2, newNode2)
                        selected_nodes[1].type = CURVE
                    if selected_nodes[0].y > selected_nodes[1].y:
                        newNode1 = GSNode(point_C)
                        newNode2 = GSNode(point_D)
                        newNode1.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[0].index + 1, newNode1)
                        newNode2.type = OFFCURVE
                        selected_path.nodes.insert(selected_nodes[0].index + 2, newNode2)
                        selected_nodes[1].type = CURVE

        if len(selected_nodes) == 1:
            distance = 100
            p1 = (selected_nodes[0].prevNode.x, selected_nodes[0].prevNode.y)
            p2 = (selected_nodes[0].x, selected_nodes[0].y)
            p3 = (selected_nodes[0].nextNode.x, selected_nodes[0].nextNode.y)
            print("p1,p2,p3:", p1, p2, p3)

            # 比较哪条线段更接近水平
            angle1, angle2 = self.compare_segments_horizontal_closeness(p1, p2, p3)
            print("angle1:", angle1)
            print("angle2:", angle2)
            if angle1 < angle2:
                horizontal_point = p1
                vertical_pont = p3
            if angle2 < angle1:
                horizontal_point = p3
                vertical_pont = p1
            print("horizontal_point:", horizontal_point)
            print("vertical_pont:", vertical_pont)
            if Glyphs.defaults['com.jingmingtao.Addpart.right'] == True:
                if horizontal_point == p1:
                    point1 = self.calculate_new_point2(p1, p2, distance)
                    point2 = self.calculate_new_point1(p2, p3, 120)
                    print("point111111:", point1)
                    print("point2222222:", point2)
                    selected_nodes[0].x = point1[0]
                    selected_nodes[0].y = point1[1]
                    distance_cross = self.distance_crosstwopoins(point1, point2) / 3
                    point3 = self.calculate_new_point1(point2, p2, distance_cross)
                    move_num = self.sqrt_diff_safe(distance_cross, 20)
                    print("move_num:", move_num)

                    if point2[1] > point1[1]:
                        print("000000000:")
                        newNode1 = GSNode((point1[0] - move_num, point1[1] + 20))
                        newNode2 = GSNode(point3)
                    if point2[1] < point1[1]:
                        newNode1 = GSNode((point1[0] - move_num, point1[1] - 20))
                        newNode2 = GSNode(point3)
                    newNode3 = GSNode((point2[0], point2[1]))
                    selected_path.nodes.insert(selected_nodes[0].index + 1, newNode3)

                    newNode1.type = OFFCURVE
                    selected_path.nodes.insert(selected_nodes[0].index + 1, newNode1)
                    newNode2.type = OFFCURVE
                    selected_path.nodes.insert(selected_nodes[0].index + 2, newNode2)
                    newNode3.type = CURVE
                    newNode3.smooth = True

                if horizontal_point == p3:
                    point1 = self.calculate_new_point2(p3, p2, distance)
                    point2 = self.calculate_new_point1(p2, p1, 120)
                    print("point1000000:", point1)
                    print("point20000000:", point2)
                    selected_nodes[0].x = point1[0]
                    selected_nodes[0].y = point1[1]
                    distance_cross = self.distance_crosstwopoins(point1, point2) / 3
                    point3 = self.calculate_new_point1(point2, p2, distance_cross)
                    move_num = self.sqrt_diff_safe(distance_cross, 20)
                    print("move_num:", move_num)

                    if point2[1] > point1[1]:
                        newNode1 = GSNode((point1[0] - move_num, point1[1] + 20))
                        newNode2 = GSNode(point3)
                    if point2[1] < point1[1]:
                        newNode1 = GSNode((point1[0] - move_num, point1[1] - 20))
                        newNode2 = GSNode(point3)
                    newNode3 = GSNode((point2[0], point2[1]))
                    selected_path.nodes.insert(selected_nodes[0].prevNode.index + 1, newNode3)
                    newNode2.type = OFFCURVE
                    selected_path.nodes.insert(newNode3.index + 1, newNode2)
                    newNode1.type = OFFCURVE
                    selected_path.nodes.insert(newNode3.index + 2, newNode1)
                    selected_nodes[0].type = CURVE
                    selected_nodes[0].smooth = True
            if Glyphs.defaults['com.jingmingtao.Addpart.left'] == True:
                if horizontal_point == p1:
                    point1 = self.calculate_new_point2(p1, p2, distance)
                    point2 = self.calculate_new_point1(p2, p3, 120)
                    print("point1yyyyy:", point1)
                    print("point2yyyyy:", point2)
                    selected_nodes[0].x = point1[0]
                    selected_nodes[0].y = point1[1]
                    distance_cross = self.distance_crosstwopoins(point1, point2) / 3
                    point3 = self.calculate_new_point1(point2, p2, distance_cross)
                    move_num = self.sqrt_diff_safe(distance_cross, 20)
                    print("move_num:", move_num)

                    if point2[1] > point1[1]:
                        newNode1 = GSNode((point1[0] + move_num, point1[1] + 20))
                        newNode2 = GSNode(point3)
                    if point2[1] < point1[1]:
                        newNode1 = GSNode((point1[0] + move_num, point1[1] - 20))
                        newNode2 = GSNode(point3)
                    newNode3 = GSNode((point2[0], point2[1]))
                    selected_path.nodes.insert(selected_nodes[0].index + 1, newNode3)

                    newNode1.type = OFFCURVE
                    selected_path.nodes.insert(selected_nodes[0].index + 1, newNode1)
                    newNode2.type = OFFCURVE
                    selected_path.nodes.insert(selected_nodes[0].index + 2, newNode2)
                    newNode3.type = CURVE
                    newNode3.smooth = True
                if horizontal_point == p3:
                    print("333333:")
                    point1 = self.calculate_new_point2(p3, p2, distance)
                    point2 = self.calculate_new_point1(p2, p1, 120)
                    print("point1:", point1)
                    print("point2:", point2)
                    selected_nodes[0].x = point1[0]
                    selected_nodes[0].y = point1[1]
                    distance_cross = self.distance_crosstwopoins(point1, point2) / 3
                    point3 = self.calculate_new_point1(point2, p2, distance_cross)
                    move_num = self.sqrt_diff_safe(distance_cross, 20)
                    print("move_num:", move_num)

                    if point2[1] > point1[1]:
                        newNode1 = GSNode((point1[0] + move_num, point1[1] + 20))
                        newNode2 = GSNode(point3)
                    if point2[1] < point1[1]:
                        newNode1 = GSNode((point1[0] + move_num, point1[1] - 20))
                        newNode2 = GSNode(point3)
                    newNode3 = GSNode((point2[0], point2[1]))
                    selected_path.nodes.insert(selected_nodes[0].prevNode.index + 1, newNode3)
                    newNode2.type = OFFCURVE
                    selected_path.nodes.insert(newNode3.index + 1, newNode2)
                    newNode1.type = OFFCURVE
                    selected_path.nodes.insert(newNode3.index + 2, newNode1)
                    selected_nodes[0].type = CURVE
                    selected_nodes[0].smooth = True

            if Glyphs.defaults['com.jingmingtao.Addpart.up']==True:
                if vertical_pont == p1:
                    print("555555555555555555555:")
                    point1 = self.calculate_new_point2(p1, p2, distance)
                    point2 = self.calculate_new_point1(p2, p3, 120)
                    print("point1:", point1)
                    print("point2:", point2)
                    selected_nodes[0].x = point1[0]
                    selected_nodes[0].y = point1[1]
                    distance_cross = self.distance_crosstwopoins(point1, point2) / 3
                    point3 = self.calculate_new_point1(point2, p2, distance_cross)
                    move_num = self.sqrt_diff_safe(distance_cross, 20)
                    print("move_num:", move_num)

                    if point2[0] > point1[0]:
                        newNode1 = GSNode((point1[0] + 20, point1[1] - move_num))
                        newNode2 = GSNode(point3)
                    if point2[0] < point1[0]:
                        newNode1 = GSNode((point1[0] - 20, point1[1] - move_num))
                        newNode2 = GSNode(point3)
                    newNode3 = GSNode((point2[0], point2[1]))
                    selected_path.nodes.insert(selected_nodes[0].index + 1, newNode3)

                    newNode1.type = OFFCURVE
                    selected_path.nodes.insert(selected_nodes[0].index + 1, newNode1)
                    newNode2.type = OFFCURVE
                    selected_path.nodes.insert(selected_nodes[0].index + 2, newNode2)
                    newNode3.type = CURVE
                    newNode3.smooth = True
                if vertical_pont == p3:
                    print("333333jjjjjjjj:")
                    point1 = self.calculate_new_point2(p3, p2, distance)
                    point2 = self.calculate_new_point1(p2, p1, 120)
                    print("point1:", point1)
                    print("point2:", point2)
                    selected_nodes[0].x = point1[0]
                    selected_nodes[0].y = point1[1]
                    distance_cross = self.distance_crosstwopoins(point1, point2) / 3
                    point3 = self.calculate_new_point1(point2, p2, distance_cross)
                    print("point3hhhh:", point3)
                    move_num = self.sqrt_diff_safe(distance_cross, 20)
                    print("move_num:", move_num)

                    if point2[0] > point1[0]:
                        print("9999999999:")
                        newNode1 = GSNode((point1[0] + 15, point1[1] - move_num))
                        newNode2 = GSNode(point3)
                    if point2[0] < point1[0]:
                        newNode1 = GSNode((point1[0] - 20, point1[1] - move_num))
                        newNode2 = GSNode(point3)
                    newNode3 = GSNode((point2[0], point2[1]))
                    selected_path.nodes.insert(selected_nodes[0].prevNode.index + 1, newNode3)
                    newNode2.type = OFFCURVE
                    selected_path.nodes.insert(newNode3.index + 1, newNode2)
                    newNode1.type = OFFCURVE
                    selected_path.nodes.insert(newNode3.index + 2, newNode1)
                    selected_nodes[0].type = CURVE
                    selected_nodes[0].smooth = True

            if Glyphs.defaults['com.jingmingtao.Addpart.down'] == True:
                if vertical_pont == p1:
                    print("555555555555555555555:")
                    point1 = self.calculate_new_point2(p1, p2, distance)
                    point2 = self.calculate_new_point1(p2, p3, 120)
                    print("point1:", point1)
                    print("point2:", point2)
                    selected_nodes[0].x = point1[0]
                    selected_nodes[0].y = point1[1]
                    distance_cross = self.distance_crosstwopoins(point1, point2) / 3
                    point3 = self.calculate_new_point1(point2, p2, distance_cross)
                    print("point3hhhh:", point3)
                    move_num = self.sqrt_diff_safe(distance_cross, 20)
                    print("move_num:", move_num)

                    if point2[0] > point1[0]:
                        newNode1 = GSNode((point1[0] + 10, point1[1] + move_num))
                        newNode2 = GSNode(point3)
                    if point2[0] < point1[0]:
                        newNode1 = GSNode((point1[0] - 10, point1[1] + move_num))
                        newNode2 = GSNode(point3)
                    newNode3 = GSNode((point2[0], point2[1]))
                    selected_path.nodes.insert(selected_nodes[0].index + 1, newNode3)

                    newNode1.type = OFFCURVE
                    selected_path.nodes.insert(selected_nodes[0].index + 1, newNode1)
                    newNode2.type = OFFCURVE
                    selected_path.nodes.insert(selected_nodes[0].index + 2, newNode2)
                    newNode3.type = CURVE
                    newNode3.smooth = True
                if vertical_pont == p3:
                    print("333333:")
                    point1 = self.calculate_new_point2(p3, p2, distance)
                    point2 = self.calculate_new_point1(p2, p1, 120)
                    print("point1:", point1)
                    print("point2:", point2)
                    selected_nodes[0].x = point1[0]
                    selected_nodes[0].y = point1[1]
                    distance_cross = self.distance_crosstwopoins(point1, point2) / 3
                    point3 = self.calculate_new_point1(point2, p2, distance_cross)
                    print("point3hhhh:", point3)
                    move_num = self.sqrt_diff_safe(distance_cross, 20)
                    print("move_num:", move_num)

                    if point2[0] > point1[0]:
                        print("9999999999:")
                        newNode1 = GSNode((point1[0] + 10, point1[1] + 50))
                        newNode2 = GSNode((point2[0] - 40, point2[1] + 5))
                    if point2[0] < point1[0]:
                        newNode1 = GSNode((point1[0] - 10, point1[1] + 30))
                        newNode2 = GSNode((point2[0] + 45, point2[1] + 5))
                    newNode3 = GSNode((point2[0], point2[1]))
                    selected_path.nodes.insert(selected_nodes[0].prevNode.index + 1, newNode3)
                    newNode2.type = OFFCURVE
                    selected_path.nodes.insert(newNode3.index + 1, newNode2)
                    newNode1.type = OFFCURVE
                    selected_path.nodes.insert(newNode3.index + 2, newNode1)
                    selected_nodes[0].type = CURVE
                    selected_nodes[0].smooth = True
#---------------------

    @objc.python_method
    def __file__(self):
        """Please leave this method unchanged"""
        return __file__